Imports ClubStarterKit.Web
Imports ClubStarterKit.Data
Imports System.Data
Imports System.Linq
Imports System.Data.Linq


Partial Class Statistics_Player
    Inherits System.Web.UI.Page

    Private _playerId As Integer
    Public Property PlayerId() As Integer
        Get
            If (_playerId = 0) Then
                _playerId = CType(ViewState("PlayerId"), Integer)
            End If
            Return _playerId
        End Get
        Set(ByVal value As Integer)
            ViewState("PlayerId") = value
        End Set
    End Property

    Private _player As Guid

    Public Property PlayerGuid() As Guid
        Get
            If _player = Guid.Empty Then
                _player = CType(ViewState("Playerid"), Guid)
            End If
            Return _player
        End Get
        Set(ByVal value As Guid)
            ViewState("Playerid") = value
        End Set
    End Property

    Protected Sub Page_InitComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.InitComplete
        'SeasonsDropDown.SelectedValue = Settings.CurrentSeason()
        'ObjectDataSource2.SelectParameters(1).DefaultValue = Settings.CurrentSeason()
        'FullStatsDS.SelectParameters(1).DefaultValue = Settings.CurrentSeason()
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            'PlayerGuid = New Guid(Request("Playerid"))
            PlayerId = CType(Request("playerid"), Integer)
        Catch ex As Exception
            Response.Redirect("~/CricketStats/default.aspx", True)
        End Try



        If IsPostBack = False Then

            If PlayerId > 0 Then
                ShowMembershipInfo()
                CreateCrumb()

                Using db As New CricketBLL.CricketStatsDataContext

                    BindSeasons(db)

                End Using

                BindMatchStats(Integer.Parse(SeasonsDropDown.SelectedValue))
            Else
                Response.Redirect("~/CricketStats/default.aspx", True)
            End If

        End If

    End Sub

    Private Sub BindMatchStats(ByVal seasonId As Integer)
        Using db As New CricketBLL.CricketStatsDataContext

            Dim batting = From b In db.PlayerStats _
                          Where b.Match.SeasonId = seasonId AndAlso b.PlayerId = PlayerId _
                          Select New With { _
                          .PlayerId = b.PlayerId, _
                          .Opponent = b.Match.OpponentTeam, _
                          .Date = b.Match.MatchDate.Value().ToShortDateString(), _
                          .HowOut = b.HowOut, _
                          .Mins = b.MinutesInBat, _
                          .Fours = b.FoursHit, _
                          .Sixes = b.SixesHit, _
                          .Runs = b.RunsScored, _
                          .Overs = b.OversBowled, _
                          .Maidens = b.Maidens, _
                          .RunsAgainst = b.Runs, _
                          .Wickets = b.Wickets, _
                          .NotOut = b.NotOut, _
                          .DNB = b.DNB}

            gvBatting.DataSource = batting
            gvBatting.DataBind()

            gvBowling.DataSource = From b In batting Where b.Overs > 0 Select b
            gvBowling.DataBind()

            'Bind Totals
            Dim totals = From b In batting _
                         Group By b.PlayerId _
                         Into Played = Count(), _
                         Innings = Count(b.DNB = False), _
                         NotOuts = Count(b.NotOut = True), _
                         Fours = Sum(b.Fours), _
                         Sixes = Sum(b.Sixes), _
                         Runs = Sum(b.Runs), _
                         AvRuns = Average(b.Runs), _
                         Overs = Sum(b.Overs), _
                         Maidens = Sum(b.Maidens), _
                         RunsAg = Sum(b.RunsAgainst), _
                         Wickets = Sum(b.Wickets)

            gvBattingTotals.DataSource = totals
            gvBattingTotals.DataBind()

            gvBowlingTotals.DataSource = From bt In totals Where bt.Overs > 0 Select bt
            gvBowlingTotals.DataBind()

        End Using

    End Sub

    Private Sub BindSeasons(ByVal dbContext As CricketBLL.CricketStatsDataContext)
        Dim seasons = From s In dbContext.Seasons _
                      Select s
        SeasonsDropDown.DataSource = seasons
        SeasonsDropDown.DataBind()


    End Sub

    Private Sub ShowMembershipInfo()
        'Dim meminfo As New MemberInfo(MemberInfo.Columns.Memberid, PlayerGuid)
        'PlayerName.Text = meminfo.Firstname & " " & meminfo.Lastname
        'PlayerName2.Text = meminfo.Firstname & " " & meminfo.Lastname

        Using db As New CricketBLL.CricketStatsDataContext
            Dim player = (From p In db.Players _
                         Where p.PlayerId = PlayerId _
                         Select p).Single()

            PlayerName.Text = player.PlayerName
            PlayerName2.Text = PlayerName.Text

        End Using

        'AvatarImg.ImageUrl = "~/usercontrols/avatarimagefetch.ashx?MemberID=" & PlayerId.ToString()

        'Dim MemCreationDate As DateTime = Membership.GetUser(PlayerGuid).CreationDate
        'MemSince.Text = MemCreationDate.ToString("MMMM yyyy")
    End Sub

    Private Sub CreateCrumb()

        Using db As New CricketBLL.CricketStatsDataContext
            Dim player = From t In db.Players _
                          Join ps In db.PlayerStats On t.PlayerId Equals ps.PlayerId _
                          Join ms In db.Matches On ps.MatchId Equals ms.MatchId _
                          Where t.PlayerId = PlayerId _
                          Select ms.Team

            For Each p In player
                TeamStats.Text = p.TeamName
                TeamStats.NavigateUrl = "~/CricketStats/team.aspx?teamid=" & p.TeamId.ToString()
            Next

        End Using
    End Sub

    Protected Sub SeasonsDropDown_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles SeasonsDropDown.SelectedIndexChanged
        'ObjectDataSource2.SelectParameters(1).DefaultValue = Integer.Parse(CType(sender, DropDownList).SelectedValue.ToString())
        'FullStatsDS.SelectParameters(1).DefaultValue = Integer.Parse(CType(sender, DropDownList).SelectedValue.ToString())

        BindMatchStats(Integer.Parse(SeasonsDropDown.SelectedValue))

    End Sub

End Class
